use super::render;
use crate::entity::Tags;
use crate::WEB;
use hypers::prelude::*;
use tera::Context;
use uuid::Uuid;

pub struct Admin;

#[openapi(name = "admin")]
impl Admin {
    #[get("/")]
    async fn admin(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/admin.html", &web)
    }

    #[get("new")]
    async fn new(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        match Tags::view_list_tag().await {
            Ok(tags) => web.insert("tags", &tags),
            Err(e) => Err(Error::Other(format!("can't find tags with: {:?}", e))),
        }
        render("admin/article_new.html", &web)
    }

    #[get("list")]
    async fn admin_list(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/admin_list.html", &web)
    }

    #[get("unpublished")]
    async fn unpublished(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/unpublished_list.html", &web)
    }

    #[get("article/view")]
    async fn admin_view_article(req: &mut Request,id: Query<Uuid>) -> impl Responder {
        let id = id.inner();
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/article_view.html", &web)
    }

    #[get("article/edit")]
    async fn article_edit(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/article_edit.html", &web)
    }

    #[get("tags")]
    async fn tags(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/tags.html", &web)
    }

    #[get("users")]
    async fn users(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/users.html", &web)
    }

    #[get("ip")]
    async fn visitor_ip_log(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/ip.html", &web)
    }

    #[get("notify")]
    async fn notify(req: &mut Request) -> impl Responder {
        let web = req.remove::<Context>(WEB).unwrap();
        render("admin/notify.html", &web)
    }
}
